<?php 
namespace Admin\Controller;
use Think\Controller;
class RankingController extends Controller{
	
		protected $db;
		protected function _initialize(){
			$this->db = D('ranking');
		}
		
		public function index(){
			$list = $this->db->getAll();
 
			$this->assign('data',$list);
			$this->display();
		}
		
		public function add(){
			if(IS_POST){
				if($this->db->m_add()){
					$this->success('添加成功','index');
				}else{
					$this->error($this->db->getError());
				}
			}else{
				$sorts = $this->db->get('sorts');
				$this->assign('sorts',$sorts);
				$this->display();
			}
			
		}
		
		public function edit(){
			$id = I('id');
			$data = $this->db->getAll($id);
			
			if(IS_POST){
				if($this->db->edit()){
					$this->success('修改成功');
				}else{
					if(empty($this->db->getError())){
						$this->error('修改失败，值相同');
					}else{
						$this->error($this->db->getError());
					}
					
				}
			}
			$sorts = $this->db->get('sorts');
			$this->assign('sorts',$sorts);
			$this->assign('id',$id);
			$this->assign('data',$data);
			$this->display();
		}
		
		public function del(){
			if(IS_GET){
				if($this->db->del()){
					$this->success('删除成功');
				}else{
					$this->error('删除失败');
				}
			}
		}
		
		public function pic(){
		$id = I('id');
		$data = $this->db->getAll($id);
		$data = $data['rankingpic'];
		
		if(I('get.pid')){
			//删除旧文件
			$this->db->unnk($data);
			//清除sharepic字段 达到删除效果
			$_POST['rankingPic'] = '';
			if($this->db->edit($id)){
				$this->success('删除成功');
			}else{
				$this->error($this->db->getError());
			}
		}
		
		if(IS_POST){
			$pic = $this->db->upload('rankingPic','rankingPic','封面上传失败');
			//删除旧文件
			$this->db->unnk(I('rankingPic'));
				
			$_POST['rankingPic'] = $pic[0];
			if($this->db->edit(I('id'))){
				$this->success('上传封面成功');
			}else{
				$this->error($this->db->getError());
			}
		}
		
		$this->assign('id',$id);
		$this->assign('data',$data);
		$this->display();
	}
		
}